#pragma warning disable 0659

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace JTLeigh.Tools.Development.CopySourceAsHtml
{

    /// <summary>
    /// Represents a style in an HTML stylesheet.
    /// </summary>
	[SuppressMessage("Microsoft.Usage", "CA2218:OverrideGetHashCodeOnOverridingEquals", Justification = "HtmlStyle won't be hashed.")]
	public class HtmlStyle
    {
        #region Fields

        private StringBuilder _style;

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlStyle"/> class.
        /// </summary>
        public HtmlStyle()
        {
            _style = new StringBuilder();
        }

        #endregion // Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the body of the style.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This property should only be used for XML serialization.
        /// </para>
        /// </remarks>
        public string Body
        {
            get { return _style.ToString(); }
            set { _style = new StringBuilder(value); }
        }

        /// <summary>
        /// Gets the length of the style.
        /// </summary>
        /// <value>
        /// The length of the style.
        /// </value>
        public virtual int Length
        {
            get { return _style.Length; }
        }

        #endregion // Properties

        #region Methods

        /// <summary>
        /// Appends content to the style.
        /// </summary>
        /// <param name="value">The content to append.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is 
        /// <see langword="null"/>.</exception>
        /// <remarks>
        /// <para>
        /// If the style has existing content, a single space will be appended to the existing 
        /// content before the new content.
        /// </para>
        /// </remarks>
        public void Append(string value)
        {
            Safety.AssertArgumentNotNull("value", value);

            value = value.Trim();
            if (value.Length > 0)
            {
                if (_style.Length > 0)
                {
                    _style.Append(' ');
                }
                _style.Append(value);
            }
        }

        /// <summary>
        /// Formats content and appends it to the style.
        /// </summary>
        /// <param name="format">The format string.</param>
        /// <param name="arguments">Arguments to format.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="format"/> is 
        /// <see langword="null"/>.</exception>
        /// <remarks>
        /// <para>
        /// Formatting is done using the invariant culture.
        /// </para>
        /// </remarks>
        public void AppendFormat(string format, params object[] arguments)
        {
            Safety.AssertArgumentNotNull("format", format);

            Append(String.Format(CultureInfo.InvariantCulture, format, arguments));
        }

        /// <summary>
        /// Returns the string representation of the style.
        /// </summary>
        /// <returns>A string representation of the style.</returns>
        public override string ToString()
        {
            return _style.ToString();
        }

        /// <summary>
        /// Returns whether the specified object is equal to the style.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            HtmlStyle htmlStyle;

            htmlStyle = obj as HtmlStyle;
            return ((htmlStyle != null) && ToString().Equals(htmlStyle.ToString()));
        }

        #endregion // Methods
    }

}

#pragma warning restore 0659
